package com.ahs.config;

import com.ahs.common.PageRequest;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * @author riven zhang
 * @since 2021/5/18 10:02
 */
public class BaseController<S extends IService<T>, T> {

    @Autowired
    private S service;

    @GetMapping("findById/{id}")
    public T findById(@PathVariable("id") Integer id) {
        return service.getById(id);
    }

    @DeleteMapping("delById")
    public Boolean delById(Serializable id) {
        return service.removeById(id);
    }

    @PostMapping("findByPage")
    public List<T> findByPage(@RequestBody PageRequest request) {
        Page<T> page = new Page<>(request.getCurrent(), request.getSize());
        Page<T> result = service.page(page);
        return result.getRecords();
    }

}
